package com.lo.controller;

import com.github.pagehelper.PageInfo;
import com.lo.common.*;
import com.lo.entity.User;
import com.lo.enums.EnumSex;
import com.lo.enums.EnumVip;
import com.lo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AvatarInfo avatarInfo;
    @Autowired
    private CertificateInfo certificateInfo;
//    @Autowired
//    private TeacherService teacherService;
    //登录校验
    @RequestMapping("login")
    public JsonResult login(String username, String password, HttpSession session){
        if ((username==null||"".equals(username.trim()))||
                (password==null||"".equals(password.trim()))){
            return new JsonResult(3,"用户密码为空",null);
        }
        JsonResult result = userService.login(username,password);
        User user = (User) result.getData();
        session.setAttribute("user",user);
        return result;
    }
    @RequestMapping("myClassPage")
    public JsonResult myClassPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                  @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                  Long id,String search){
        PageInfo pageInfo = userService.page(pageNum,pageSize,id, search);
        JsonResult result = new JsonResult(0,"我的课程",pageInfo);
        return result;
    }

    //审核未通过课程
    @RequestMapping("examiningCoursePage")
    public JsonResult NotExamineCoursePage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                           @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                           Long id,String search){
        PageInfo pageInfo = userService.NotExamineCoursePage(pageNum,pageSize,id,search);
        JsonResult result = new JsonResult(0,"待审核课程",pageInfo);
        return result;
    }

    @RequestMapping("myTextPages")
    public JsonResult myTextPages(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                   Long id,String search){
        PageInfo pageInfo = userService.myTextPages(pageNum,pageSize,id,search);
        return new JsonResult<>(0,"我的文本资料",pageInfo);
    }
    @RequestMapping("myExaminingTextPages")
    public JsonResult myExaminingTextPages(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                           @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                           Long id,String search){
        PageInfo pageInfo = userService.myExaminingTextPages(pageNum,pageSize,id,search);
        return new JsonResult(0,"待审核文本资料",pageInfo);
    }


    @RequestMapping("register")
    public JsonResult register(User user,HttpSession session){
        user.setAge(18);
        user.setSex(EnumSex.MALE);
        user.setVip(EnumVip.ORDINARY);
        JsonResult result = userService.register(user);

        if (result.getState()==0){
            user = (User) result.getData();
            session.setAttribute("user",user);
        }
        return result;
    }

    @RequestMapping("selectUserMessage/{username}")
    public JsonResult selectUserMessage(@PathVariable String username){
        JsonResult result = userService.selectUserMessage(username);
        return result;
    }

    @RequestMapping("updateUserMessage")
    public JsonResult updateUserMessage(User user, MultipartFile avatarFile){
        JsonResult selectResult = userService.selectUserMessage(user.getUsername());    //跟俊传过来的用户名获取用户的信息
        User userTemp = (User) selectResult.getData();
        if (userTemp.getAvatarUrl()!=null && avatarFile!=null){
            FileDeleteUtils.delete(avatarInfo.getBasePath(),userTemp.getAvatarUrl());
        }
        if (avatarFile!=null){
            JsonResult saveResult = FileUploadUtils.upload(avatarFile,avatarInfo.getBasePath(),"avatar");
            user.setAvatarUrl((String) saveResult.getData());
        }
        return userService.updateUserMessage(user);
    }

    //搜索所用用户
    @RequestMapping("selectAllUsers")
    public JsonResult selectAllUsers(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                     String search){
        PageInfo pageInfo = userService.selectAllUsers(pageNum,pageSize,search);
        JsonResult result = new JsonResult(0,"用户列表",pageInfo);
        return  result;
    }
    @RequestMapping("selectAllTeachers")
    public JsonResult selectAllTeachers(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                     String search){
        PageInfo pageInfo = userService.selectAllTeachers(pageNum,pageSize,search);
        JsonResult result = new JsonResult(0,"用户列表",pageInfo);
        return  result;
    }
    //传入信息设置封禁
    @RequestMapping("setUserBanned/{id}")
    public JsonResult setUserBanned(@PathVariable Long id){
        return userService.setUserBanned(id);
    }
    //传入信息解封
    @RequestMapping("setUserEnable/{id}")
    public JsonResult setUserEnable(@PathVariable Long id){
        return userService.setUserEnable(id);
    }

    @RequestMapping("userCount")
    public JsonResult userCount(){
        return userService.userCount();
    }

    @RequestMapping("userOrdinaryCount")
    public JsonResult userOrdinaryCount(){
        return userService.userOrdinaryCount();
    }

    @RequestMapping("userVipCount")
    public JsonResult userVipCount(){
        return userService.userVipCount();
    }

    //教师数量统计迁移api
    @RequestMapping("teacherCount")
    public JsonResult teacherCount(){
        return userService.teacherCount();
    }

    @RequestMapping("teacherOrdinaryCount")
    public JsonResult teacherOrdinaryCount(){
        return userService.teacherOrdinaryCount();
    }

    @RequestMapping("teacherVipCount")
    public JsonResult teacherVipCount(){
        return userService.teacherVipCount();
    }
    //////////////
    @RequestMapping("refuseToTeacher/{id}")
    public JsonResult refuseToTeacher(@PathVariable Long id){
        return userService.refuseToTeacher(id);
    }
    @RequestMapping("launchToTeacher")
    public JsonResult launchToTeacher(User user,MultipartFile certificateFile){
        JsonResult saveResult = FileUploadUtils.upload(certificateFile,certificateInfo.getBasePath(),"certificate");
        user.setCertificate((String) saveResult.getData());
        return userService.launchToTeacher(user);
    }
    @RequestMapping("acceptToTeacher/{id}")
    public JsonResult acceptToTeacher(@PathVariable Long id){
        return userService.acceptToTeacher(id);
    }
    @RequestMapping("launchingTeacher")
    public JsonResult launchingTeacher(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                       @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                       String search){
        PageInfo pageInfo = userService.launchingTeacher(pageNum,pageSize,search);
        JsonResult result = new JsonResult(0,"认证列表",pageInfo);
        return  result;
    }

    @RequestMapping("signOutUser/{id}")
    public JsonResult signOutUser(@PathVariable Long id){
        return userService.signOutUser(id);
    }

    @RequestMapping("resetPassword")
    public JsonResult resetPassword(User user){
        JsonResult userResult = userService.resetUserPassword(user);
//        JsonResult teacherResult = teacherService.resetUserPassword(user);
        if (userResult.getState()==0 ){
            return new JsonResult(0,"密码重置成功",null);
        }
        return new JsonResult(3,"密码重置失败，联系管理员",null);
    }

    //关注接口
    @RequestMapping("isFollowed")
    public JsonResult isFollowed(@RequestParam("followId")Long followId,@RequestParam("userId") Long userId){
        return userService.isFollowed(followId,userId);
    }
    @RequestMapping("follow")
    public JsonResult follow(@RequestParam("followId")Long followId,@RequestParam("userId") Long userId){
        return userService.follow(followId,userId);
    }
    @RequestMapping("unFollow")
    public JsonResult unFollow(@RequestParam("followId")Long followId,@RequestParam("userId") Long userId){
        return userService.unFollow(followId,userId);
    }
    @RequestMapping("myFollows")
    public JsonResult myFollows(@RequestParam("userId")Long userId){
        return userService.myFollows(userId);
    }
    @RequestMapping("myFans")
    public JsonResult myFans(@RequestParam("userId")Long userId){
        return userService.myFans(userId);
    }
    @RequestMapping("myFollowCount")
    public JsonResult myFollowCount(@RequestParam("userId")Long userId){
        return userService.myFollowCount(userId);
    }
    @RequestMapping("myFansCount")
    public JsonResult myFansCount(@RequestParam("userId")Long userId){
        return userService.myFansCount(userId);
    }

    @RequestMapping("selectUserById")
    public JsonResult selectUserById(Long id){
        return userService.selectUserById(id);
    }

    @RequestMapping("selectMyFollowCourse")
    public JsonResult selectMyFollowCourse(Long userId){
        System.out.println(userId);
        return userService.selectMyFollowCourse(userId);

    }
}
